package com.i2863.shiroDemo.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 公共字段自动填充
 */

/**
 * @author cexy
 * @date 2021/5/17 10:40
 * @description  公共字段自动填充
 **/
@Component
public class AutoMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        Object updateTime = getFieldValByName("updateTime", metaObject);
        if(metaObject.hasGetter("updateTime") && updateTime == null){
            setFieldValByName("updateTime", new Date(), metaObject);
        }

        Object createTime = getFieldValByName("createTime", metaObject);
        if(metaObject.hasGetter("createTime") && createTime == null) {
            setFieldValByName("createTime", new Date(), metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        setFieldValByName("updateTime", new Date(), metaObject);
    }

}